package com.wtg.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.Arrays;

public class CustomerMd5AuthorizeRealm extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授予权限");
        String username = (String) principalCollection.getPrimaryPrincipal();
        if ("admin".equals(username)){
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.addRoles(Arrays.asList("super","user"));
            info.addStringPermissions(Arrays.asList("user:update:*","massage:read:001"));
            return info;
        }
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        if ("admin".equals(username)){
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, "cfabb878c018a0a4dc11056cba30628e", ByteSource.Util.bytes("aaabbb"), this.getName());
            return info;
        }
        return null;
    }
}
